<template>
	<el-dialog :title="form.id ? '编辑' : '新增'" v-model="visible" :close-on-click-modal="false" draggable>
		<el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
			<el-row :gutter="24">
				<el-col :span="12" class="mb20">
					<el-form-item label="主任务" prop="mainTaskId">
						<el-select
							v-model="form.mainTaskId"
							filterable
							remote
							reserve-keyword
							placeholder="请输入主任务名称"
							:remote-method="getProjectList"
							style="width: 240px"
							@change="selectChange"
						>
							<el-option v-for="item in mainTaskList" :key="item.id" :label="item.taskName" :value="item.id" />
						</el-select>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="任务类型" prop="taskType">
						<el-input v-model="form.taskType" placeholder="请输入任务类型" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="任务名称" prop="taskName">
						<el-input v-model="form.taskName" placeholder="请输入任务名称" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="任务状态" prop="taskStatus">
						<el-select v-model="form.taskStatus" placeholder="请选择任务状态" @change="taskStatusChange">
							<el-option :key="index" :label="item.label" :value="item.value" v-for="(item, index) in task_status"></el-option>
						</el-select>
					</el-form-item>
				</el-col>

				<el-col :span="24" class="mb20">
					<el-form-item label="任务描述" prop="taskDesc">
						<el-input type="textarea" v-model="form.taskDesc" placeholder="请输入任务描述" />
					</el-form-item>
				</el-col>

				<el-col :span="24" class="mb20">
					<el-form-item label="任务内容" prop="taskContent">
						<Editor v-model:getHtml="form.taskContent" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="负责人" prop="commander">
						<UserSelect placeholder="负责人" v-model="form.commander" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="开始时间" prop="startTime">
						<el-date-picker type="date" placeholder="请选择开始时间" v-model="form.startTime" :value-format="dateStr"></el-date-picker>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="结束时间" prop="endTime">
						<el-date-picker type="date" placeholder="请选择结束时间" v-model="form.endTime" :value-format="dateStr"></el-date-picker>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="预估工时" prop="estimatedWorkingHours">
						<el-input v-model="form.estimatedWorkingHours" placeholder="请输入预估工时" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="实际工时" prop="realityWorkingHours">
						<el-input v-model="form.realityWorkingHours" placeholder="请输入实际工时" />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">取消</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts" name="ProjectSubTaskDialog">
import { useMessage } from '@/hooks/message';
import { getObj, addObj, putObj } from '@/api/oa/projectSubTask';
import { useDict } from '@/hooks/dict';
import UserSelect from '@/components/UserSelect/index.vue';
import { fetchList as fetchMainTaskList } from '@/api/oa/projectMainTask';
const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);
const mainTaskList = ref();
// 定义字典
const { task_status } = useDict('task_status');

// 提交表单数据
const form = reactive({
	id: '',
	mainTaskId: '',
	taskType: '',
	taskName: '',
	taskDesc: '',
	taskContent: '',
	taskStatus: '',
	commander: '',
	startTime: '',
	endTime: '',
	estimatedWorkingHours: '',
	realityWorkingHours: '',
});

// 定义校验规则
const dataRules = ref({
	mainTaskId: [{ required: true, message: '主任务ID不能为空', trigger: 'blur' }],
	taskType: [{ required: true, message: '任务类型不能为空', trigger: 'blur' }],
	taskName: [{ required: true, message: '任务名称不能为空', trigger: 'blur' }],
	taskDesc: [{ required: true, message: '任务描述不能为空', trigger: 'blur' }],
	taskContent: [{ required: true, message: '任务内容不能为空', trigger: 'blur' }],
	taskStatus: [{ required: true, message: '任务状态', trigger: 'blur' }],
	commander: [{ required: true, message: '负责人不能为空', trigger: 'blur' }],
	startTime: [{ required: true, message: '开始时间不能为空', trigger: 'blur' }],
	endTime: [{ required: true, message: '结束时间不能为空', trigger: 'blur' }],
	estimatedWorkingHours: [{ required: true, message: '预估工时不能为空', trigger: 'blur' }],
	realityWorkingHours: [{ required: false, message: '实际工时不能为空', trigger: 'blur' }],
});

const selectChange = async (e: any) => {
	form.value.taskName = mainTaskList.value.find((item: any) => item.taskName == e).taskName;
};
const getProjectList = async (p: string) => {
	p = p.toUpperCase();
	let params = {
		size: 200,
		taskName: p,
		taskStatus: 'doing',
	};
	fetchMainTaskList(params)
		.then((res: any) => {
			mainTaskList.value = res.data.records;
		})
		.finally(() => {});
};

const taskStatusChange = (e: any) => {
	if (e == 'closed') {
		dataRules.value.realityWorkingHours[0].required = true;
	} else {
		dataRules.value.realityWorkingHours[0].required = false;
	}
};

// 打开弹窗
const openDialog = (id: string) => {
	visible.value = true;
	form.id = '';

	// 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});

	// 获取projectSubTask信息
	if (id) {
		form.id = id;
		getprojectSubTaskData(id);
	}
};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
		loading.value = true;
		form.id ? await putObj(form) : await addObj(form);
		useMessage().success(form.id ? '修改成功' : '添加成功');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
		loading.value = false;
	}
};

// 初始化表单数据
const getprojectSubTaskData = (id: string) => {
	// 获取数据
	loading.value = true;
	getObj(id)
		.then((res: any) => {
			Object.assign(form, res.data);
		})
		.finally(() => {
			loading.value = false;
		});
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
